package com.meituan.meituanservice.controller;

import com.meituan.meituanservice.model.User;
import com.meituan.meituanservice.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

@RestController
@CrossOrigin

public class UserController {
    @Autowired
    UserService userService;
    @PostMapping("login")
    public Map<String,Object> login(
            @RequestBody User user){
            Map<String,Object> map = new HashMap<>();
            User result = userService.login(user.getNick(),user.getPassword());
            if(result!=null){
                map.put("userInfo",result);
            }else{
                map.put("userInfo",null);
            }
    return map;
    }

    @PostMapping("register")
    public Map<String,Integer> register(@RequestBody User user){
        Map<String,Integer> map = new HashMap<>();
        Boolean result = userService.register(user);
        if(result){
            map.put("status",0);
        }else{
            map.put("status",1);
        }
        return map;
    }

    @GetMapping("getUser")
    public Map getUser(String nick){
        Map map =new HashMap();
        User user = userService.checkByNick(nick);
        if(user!=null){
            map.put("err_code",0);
            map.put("user",user);
        }else{
            map.put("err_code",1);
        }
        return map;
    }

    @PostMapping("updatePhone")
    public Map<String,Integer>  updatePhone(@RequestBody LinkedHashMap obj){
        Map<String,Integer>  map = new HashMap<>();
        String newPhone = (String) obj.get("newPhone");
        String oldPhone = (String) obj.get("oldPhone");
        Integer userId = (Integer) obj.get("userId");
        if(userService.updatePhone(userId,newPhone,oldPhone)==1){
            map.put("err_code",0);
        }else{
            map.put("err_code",1);
        }
        return map;
    }

    @PostMapping("updatePayCode")
    public Map<String,Integer>  updatePayCode(@RequestBody LinkedHashMap obj){
        Map<String,Integer>  map = new HashMap<>();
        String newPayCode = (String) obj.get("newPayCode");
        String oldPayCode = (String) obj.get("oldPayCode");
        Integer userId = (Integer) obj.get("userId");
        if(userService.updatePayCode(userId,newPayCode,oldPayCode)==1){
            map.put("err_code",0);
        }else{
            map.put("err_code",1);
        }
        return map;
    }

    @PostMapping("uploadAvatar")
    public Map<String,Integer> uploadAvatar(MultipartFile file,String userId){
        Map<String ,Integer> map = new HashMap<>();
        String fileName = file.getOriginalFilename();
        String fileSuffix =fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String format = sdf.format(new Date());
        String finalName = format+'.'+fileSuffix;
        String imgPath = "/static/avatar?filename="+finalName;
        userService.updateAvatar(imgPath,userId);
        String diskPath = "G:\\meituanUpload\\userAvatar";
        File localFile = new File(diskPath,finalName);
        if(!localFile.getParentFile().exists()){
            localFile.getParentFile().mkdirs();
        }
        try {
            file.transferTo(localFile);
            map.put("err_code",0);
        } catch (IOException e) {
            e.printStackTrace();
            map.put("err_code",1);
        }
        return map;
    }

    @PostMapping("doPay")
    public Map doPay(String userId,String payCode,String orderId){
        Map<String ,Integer> map = new HashMap<>();
        String result = userService.verifyPayCode(userId);
        if(payCode.equals(result)){
            RestTemplate restTemplate = new RestTemplate();
            Integer res = restTemplate.getForObject("http://127.0.0.1:8082/doPay?orderId="+orderId,Integer.class);
            if(res!=null){
                if(res==1){
                    map.put("err_code",0);
                }
            }else{
                map.put("err_code",2);
            }
        }else{
            map.put("err_code",1);
        }
        return map;
    }

    @PostMapping("doOver")
    public Map doOver(String userId,String payCode,String orderId){
        Map<String ,Integer> map = new HashMap<>();
        String result = userService.verifyPayCode(userId);
        if(payCode.equals(result)){
            RestTemplate restTemplate = new RestTemplate();
            Integer res = restTemplate.getForObject("http://127.0.0.1:8082/doOver?orderId="+orderId,Integer.class);
            if(res!=null){
                if(res==1){
                    map.put("err_code",0);
                }
            }else{
                map.put("err_code",2);
            }
        }else{
            map.put("err_code",1);
        }
        return map;
    }



}
